clear all
set more off

global datapath /INSERTPATH
global codepath /INSERTPATH
global resultspath /INSERTPATH

local countrysetname all

/* load dataset */
use "$datapath/full_dataset.dta", clear

/* additional cleaning steps */ 
run "$codepath/prep_regression_amadeus.do"

/* create ids and variables */
egen firm_id = group(bvdid)
egen county_id = group(county_unique)
egen state_id = group(statecode)
encode country, gen(countrycode)

gen ln_revenue = ln(revenue)
gen ln_numberofemployees = ln(numberofemployees)
gen ln_revenue_worker = ln(revenue/numberofemployees)
gen ln_gdp_pc = ln(gdp_per_capita_real_PPP)

keep if ~missing(ln_revenue_worker)

/* generate sampling weights for some specifications */
bysort country: gen samplingweights = 1/[_N]
gen GDP_and_sampling_weights = samplingweights*world_GDP_share
gen emp_and_sampling_weights = samplingweights*world_employment_share

/* choose set of countries, temperature transformation, sector, dependent variable, interaction, and weighting */
local countrysetname all
local transforms polyabove1 /*polyabove2 bins poly4*/
local sectors manu /*serv all*/
local lhsvars ln_revenue_worker /*ln_revenue ln_numberofemployees numberofemployees*/
local interactions interacted /*uninteracted*/
local weightings GDP pop unweighted 
local time_fe country_year_fe state_year_fe

/* assemble rhs variables according to options chosen above */ 

foreach weights of local weightings {
foreach answer of local interactions {
foreach transform of local transforms {
if "`transform'" == "bins" & "`answer'" == "uninteracted" {
local rhsvars bin_5C_35_Inf bin_5C_30_35 bin_5C_25_30 ///
bin_5C_20_25 bin_5C_10_15 bin_5C_5_10 bin_5C_0_5 bin_5C_n5_0 bin_5C_n10_n5 bin_5C_nInf_n10
}
if "`transform'" == "bins" & "`answer'" == "interacted" {
local rhsvars bin_5C_35_Inf bin_5C_30_35 bin_5C_25_30 ///
bin_5C_20_25 bin_5C_10_15 bin_5C_5_10 bin_5C_0_5 bin_5C_n5_0 bin_5C_n10_n5 bin_5C_nInf_n10 ///
c.bin_5C_35_Inf#c.avg_tmax_lr c.bin_5C_30_35#c.avg_tmax_lr c.bin_5C_25_30#c.avg_tmax_lr ///
c.bin_5C_20_25#c.avg_tmax_lr c.bin_5C_10_15#c.avg_tmax_lr c.bin_5C_5_10#c.avg_tmax_lr ///
c.bin_5C_0_5#c.avg_tmax_lr c.bin_5C_n5_0#c.avg_tmax_lr c.bin_5C_n10_n5#c.avg_tmax_lr ///
c.bin_5C_nInf_n10#c.avg_tmax_lr ///
c.bin_5C_35_Inf#c.ln_gdp_pc c.bin_5C_30_35#c.ln_gdp_pc ///
c.bin_5C_25_30#c.ln_gdp_pc c.bin_5C_20_25#c.ln_gdp_pc ///
c.bin_5C_10_15#c.ln_gdp_pc c.bin_5C_5_10#c.ln_gdp_pc ///
c.bin_5C_0_5#c.ln_gdp_pc c.bin_5C_n5_0#c.ln_gdp_pc ///
c.bin_5C_n10_n5#c.ln_gdp_pc c.bin_5C_nInf_n10#c.ln_gdp_pc
}
if "`transform'" == "poly1" {
local rhsvars poly_1
}
if "`transform'" == "poly2" {
local rhsvars poly_1 poly_2
}
if "`transform'" == "poly3" {
local rhsvars poly_1 poly_2 poly_3
}
if "`transform'" == "poly4" & "`answer'" == "uninteracted" {
local rhsvars poly_1 poly_2 poly_3 poly_4
}
if "`transform'" == "poly4" & "`answer'" == "interacted" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr
}
if "`transform'" == "polyabove1" & "`answer'" == "uninteracted" {
local rhsvars polyBelow5_1 polyAbove30_1
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr
}
if "`transform'" == "polyabove2" & "`answer'" == "uninteracted" {
local rhsvars polyBelow5_2 polyAbove30_2
}
if "`transform'" == "polyabove2" & "`answer'" == "interacted" {
local rhsvars polyBelow5_2 polyAbove30_2 ///
c.polyBelow5_2#c.ln_gdp_pc c.polyAbove30_2#c.ln_gdp_pc ///
c.polyBelow5_2#c.avg_tmax_lr c.polyAbove30_2#c.avg_tmax_lr
}

if "`time_fe'" == "country_year_fe" {
	local time_fixed_effect countrycode#year
}

if "`time_fe'" == "state_year_fe" {
	local time_fixed_effect statecode#year
}


/* run regressions */ 

foreach sector of local sectors {
foreach lhsvar of local lhsvars {
if "`sector'" == "all" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker), ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker) & ~missing(avg_tmax_lr) ///
& ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker) & ~missing(avg_tmax_lr) ///
& ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
}
if "`sector'" == "manu" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker), ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
}
if "`sector'" == "serv" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker), ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id `time_fixed_effect') cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'.ster", replace
}
}
}
}
}
}
}

